package pers.xiaojun.boot.module.infra.controller.file;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.pojo.PageResult;
import pers.xiaojun.boot.module.infra.controller.file.vo.config.FileConfigQueryReqVO;
import pers.xiaojun.boot.module.infra.controller.file.vo.config.FileConfigRespVO;
import pers.xiaojun.boot.module.infra.controller.file.vo.config.FileConfigSaveReqVO;
import pers.xiaojun.boot.module.infra.dao.entity.FileConfigDO;
import pers.xiaojun.boot.module.infra.service.file.FileConfigService;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

/**
 * 文件配置控制层
 *
 * @author xiaojun
 * @since 2025-11-13
 */
@Tag(name = "文件配置")
@AdminApi
@RestController
@RequestMapping("/infra/file-config")
@RequiredArgsConstructor
public class FileConfigController {

    private final FileConfigService fileConfigService;

    @PostMapping
    @Operation(summary = "创建文件配置")
    @PreAuthorize("@ss.hasPermission('infra:file-config:create')")
    public CommonResult<?> createFileConfig(@Valid @RequestBody FileConfigSaveReqVO createVO) {
        return CommonResult.success(fileConfigService.saveFileConfig(createVO).getId());
    }

    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('infra:file-config:update')")
    @Operation(summary = "更新文件配置")
    @Parameter(name = "id", description = "配置Id", required = true)
    public CommonResult<?> updateFileConfig(@PathVariable("id") Long id, @Valid @RequestBody FileConfigSaveReqVO updateVO) {
        updateVO.setId(id);
        return CommonResult.success(fileConfigService.updateFileConfig(updateVO).getId());
    }

    @PutMapping("/{id}/set-master-config")
    @PreAuthorize("@ss.hasPermission('infra:file-config:update')")
    @Operation(summary = "设置文件配置为主配置")
    @Parameter(name = "id", description = "配置Id", required = true)
    public CommonResult<?> updateFileConfigAsMaster(@PathVariable("id") Long id) {
        fileConfigService.updateFileConfigAsMaster(id);
        return CommonResult.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('infra:file-config:delete')")
    @Operation(summary = "删除文件配置")
    @Parameter(name = "id", description = "配置Id", required = true)
    public CommonResult<?> deleteFileConfig(@PathVariable("id") Long id) {
        fileConfigService.removeFileConfigById(id);
        return CommonResult.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
    @Operation(summary = "获取文件配置")
    @Parameter(name = "id", description = "配置Id", required = true)
    public CommonResult<FileConfigRespVO> getFileConfig(@PathVariable("id") Long id) {
        return CommonResult.success(BeanUtil.toBean(fileConfigService.getFileConfigById(id), FileConfigRespVO.class));
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
    @Operation(summary = "获取文件配置分页")
    public CommonResult<PageResult<FileConfigRespVO>> getFileConfigPage(@Valid FileConfigQueryReqVO queryVO) {
        Page<FileConfigDO> page = fileConfigService.listFileConfigsPage(queryVO);
        return CommonResult.success(PageConvert.INSTANCE.toPageResult(page, FileConfigRespVO.class));
    }

    @GetMapping("/{id}/test")
    @PreAuthorize("@ss.hasPermission('infra:file-config:test')")
    @Operation(summary = "测试文件配置连接")
    @Parameter(name = "id", description = "配置Id", required = true)
    public CommonResult<PageResult<FileConfigRespVO>> testFileConfigConnect(@PathVariable("id") Long id) {
        fileConfigService.testFileConfigConnect(id);
        return CommonResult.success();
    }

}
